﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CCNew : ICPRelationshipPage
{
    public CCData ccData = new CCData();
    #region Properties
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Courtesy Call - Create New";
        if (!IsPostBack && !IsCallback)
        {
        }
    }
    protected void cbkCCNew_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = CreateNewCC(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    private string CreateNewCC(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxReturn = String.Empty;

        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            if (sxRowSplitted[0].ToUpper().Contains(KCC.CP_ID))
            {
                String[] sxIDSplitted = sxRowSplitted[1].Split('_');
                bool IsNewCP = sxIDSplitted[1].ToUpper() == KGlobal.YES;

                int iCPId = Convert.ToInt32(sxIDSplitted[0]);
                if (IsNewCP)
                {
                    ccData.iNewContactPersonID = iCPId;
                }
                else
                {
                    ccData.iContactPersonID = iCPId;
                }
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCC.CUSTOMER_ID))
            {
                ccData.iCustomerID = Convert.ToInt32(sxRowSplitted[1]);
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCC.CALL_TYPE))
            {
                ccData.sxCallStatus = sxRowSplitted[1];
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCC.PLANNED_DATE))
            {
                if (ccData.sxCallStatus == "DIRECT")
                {
                    ccData.dtmPlannedDate = DateTime.Today;
                }
                else
                {
                    ccData.dtmPlannedDate = Convert.ToDateTime(sxRowSplitted[1]);
                }
            }
        }

        DBQuery.CourtesyCallTable.SaveCall(ccData).ToString();
        sxReturn = ccData.iCCID.ToString();

        if (ccData.sxCallStatus.ToUpper() == "PLANNED")
        {
            DBQuery.NotificationTable.InsertNotification_UserEvent(sxReturn, ICPUser.ID, ccData.iCustomerID,
                UserEventType.CourtessyCall, ccData.dtmPlannedDate);
        }
        
        return sxReturn;
    }
    #endregion Custom Methods
}
